﻿#include "inputstringsdialog.h"
#include "ui_inputstringsdialog.h"
#include <QStringListModel>
#include <QMessageBox>

InputStringsDialog::InputStringsDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::InputStringsDialog)
{
    ui->setupUi(this);
}

InputStringsDialog::~InputStringsDialog()
{
    delete ui;
}

void InputStringsDialog::setInputStrings(const QString &inputString)
{
    QStringList inputStrings = inputString.split(';', QString::SkipEmptyParts);
    ui->listWidget->addItems(inputStrings);
}

QString InputStringsDialog::getResult()
{
    QStringList inputStrings;
    for (int i = 0; i < ui->listWidget->count(); i++)
        inputStrings.append(ui->listWidget->item(i)->text());
    return inputStrings.join(';');
}

void InputStringsDialog::on_btnAdd_clicked()
{
    if (ui->textEdit->toPlainText().isEmpty())
        return;
    ui->listWidget->addItem(ui->textEdit->toPlainText());
    ui->textEdit->clear();
}

void InputStringsDialog::on_btnRemove_clicked()
{
    if (ui->listWidget->currentRow() != -1) {
        if (QMessageBox::question(this, windowTitle(), QStringLiteral("确定要删除这一项吗？")) == QMessageBox::Yes) {
            int currentIndex = ui->listWidget->currentRow();
            QListWidgetItem* itemToDel = ui->listWidget->takeItem(currentIndex);
            delete itemToDel;
        }
    }
}

void InputStringsDialog::on_listWidget_itemDoubleClicked(QListWidgetItem *item)
{
    ui->textEdit->setText(item->text());
    ui->textEdit->selectAll();
    ui->textEdit->setFocus();
}
